require 'FileUtils'
require 'rubygems'
class InsdbController < ApplicationController

	
  def load_into_database

    @errors = []
    
    if request.post?
     @sel_file=params[:xml_file]
     
       data = @sel_file.read if @sel_file.respond_to? :read
         if data
             
            doc = REXML::Document.new( data )
            
            REXML::XPath.each( doc, "//ReturnedDebitItem" ){ |ret_debit_element|
            retd_iref = ret_debit_element.attributes["ref"].rstrip
            retd_valueof = ret_debit_element.attributes["valueOf"]
            retd_oriprocdt = ret_debit_element.attributes["originalProcessingDate"]  
                     
            retd_name = String.new
            
            ret_debit_element.each_element{ |child|
            retd_name = child.attributes["name"].rstrip
            }
            
            temp_retd_iref = retd_iref
            
		
		# test using 9 digit reference
		str_account = temp_retd_iref[retd_iref.length-9,retd_iref.length]

		# Use full ref for description

            dstr_neg = retd_iref
            
            dstr_pos = retd_iref+" "+retd_name

		# dstr_neg = temp_retd_iref[retd_iref.length-9,retd_iref.length]
            
            # dstr_pos = temp_retd_iref[retd_iref.length-9,retd_iref.length]+" "+retd_name
            
            # str_account = dstr_pos.rstrip
            # dstr_pos.rstrip is concatenated from SPACE and retd_name so cannot test for account should be dstr_neg.rstrip

		# use full reference so test now above
            # str_account = dstr_neg.rstrip
            
            s_account = " "
            
            if str_account.length == 9 and str_account[0,2]=="71" and /\d[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/.match(str_account)
             s_account = "B6004"
            end
            
            if str_account.length == 9 and str_account[0,2]=="11" and /\d[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/.match(str_account)
             s_account = "B6004"
            end
            
            if str_account.include? " "
             s_account = "B6004"
            end   
            
            dim1_str = " "
            
            if s_account == "B6004"
              dim1_str = " "            
            end
                               
		         xmlinfobif = Xmlinfobif.new(:batch_id1=> "rdd",:interface=> "BI",:voucher_type=> "BP",:trans_type=> "GL",:client=> "EH",:account=> "B5401",:dim_1=> " ",:tax_code=> "0",:tax_system=> " ",:currency=> "GBP",:cur_amount=> ("-"+retd_valueof).to_f,:amount=> ("-"+retd_valueof).to_f, :description=> dstr_neg, :trans_date=> retd_oriprocdt, :voucher_date=> retd_oriprocdt, :voucher_no=> 1, :period=> params[:arg1].to_i) 
		         xmlinfobif.save
		         
		         xmlinfobif1 = Xmlinfobif.new(:batch_id1=> "rdd",:interface=> "BI",:voucher_type=> "BP",:trans_type=> "GL",:client=> "EH",:account=> s_account,:dim_1=> dim1_str,:tax_code=> "0",:tax_system=> " ",:currency=> "GBP",:cur_amount=> retd_valueof.to_f,:amount=> retd_valueof.to_f, :description=> dstr_pos, :trans_date=> retd_oriprocdt, :voucher_date=> retd_oriprocdt, :voucher_no=> 1, :period=> params[:arg1].to_i)   
		         xmlinfobif1.save
             
            } 

          end    
                
                  
                  
    end
        redirect_to :action => :index,:controller=>'xmlbifinfo'
  
    
  end
  
  def output_bif
      bifstr=String.new
      xmlinfobif1 = Xmlinfobif.new
      cur_at = 0
    Xmlinfobif.find(:all).each do |xmlinfobif| 
     cur_a = xmlinfobif.cur_amount * 100
     cur_at = cur_at + cur_a
     xmlinfobif1 = xmlinfobif
     end
 
    st_dt=String.new 
    t_dt =  String.new  
    t_dt << xmlinfobif1.trans_date.to_s

    if !t_dt.empty?

    at_dt = t_dt.split("-")
    st_dt << at_dt[0]+at_dt[1]+at_dt[2]

    else
    st_dt << " "
    end
    

    sv_dt=String.new 
    v_dt =  String.new  
    v_dt << xmlinfobif1.voucher_date.to_s
    if !v_dt.empty?
    av_dt = v_dt.split("-")
    sv_dt << av_dt[0]+av_dt[1]+av_dt[2]
    else
    sv_dt << " "
    end
    
    # The line below is a left over from bifBOTka so I have removed it.    
    # bifstr << xmlinfobif1.batch_id1.ljust(25," ")+xmlinfobif1.interface.ljust(25," ")+xmlinfobif1.voucher_type.ljust(25," ")+xmlinfobif1.trans_type.ljust(2," ")+xmlinfobif1.client.ljust(25," ")+"R6101".ljust(25," ")+"3280".ljust(25," ")+" ".ljust(150," ")+xmlinfobif1.tax_code.ljust(25," ")+xmlinfobif1.tax_system.ljust(25," ")+xmlinfobif1.currency.ljust(25," ")+"  "+cur_at.to_i.to_s.rjust(20," ")+cur_at.to_i.to_s.rjust(20," ")+" ".ljust(71," ")+("Rent Allowances Batch Import "+params[:arg2]).ljust(255," ")+st_dt.ljust(8," ")+sv_dt.ljust(8," ")+xmlinfobif1.voucher_no.to_s.ljust(15," ")+xmlinfobif1.period.to_s.ljust(6," ")+" ".ljust(1277," ")+"\n"  
   
    Xmlinfobif.find(:all).each do |xmlinfobif| 
     cur_a = xmlinfobif.cur_amount * 100

    st_dt1=String.new 
    t_dt1 =  String.new      
    t_dt1 << xmlinfobif.trans_date.to_s
    if !t_dt1.empty? 
    at_dt1 = t_dt1.split("-")
    st_dt1 << at_dt1[0]+at_dt1[1]+at_dt1[2]
    else
    st_dt1 << " "
    end
   
    sv_dt1=String.new 
    v_dt1 =  String.new 
    v_dt1 << xmlinfobif.voucher_date.to_s
    if !v_dt1.empty?     
    av_dt1 = v_dt1.split("-")
    sv_dt1 << av_dt1[0]+av_dt1[1]+av_dt1[2]
    else
      sv_dt1 << " "  
    end
    bifstr << xmlinfobif.batch_id1.ljust(25," ")+xmlinfobif.interface.ljust(25," ")+xmlinfobif.voucher_type.ljust(25," ")+xmlinfobif.trans_type.ljust(2," ")+xmlinfobif.client.ljust(25," ")+xmlinfobif.account.ljust(25," ")+xmlinfobif.dim_1.ljust(25," ")+" ".ljust(150," ")+xmlinfobif.tax_code.ljust(25," ")+xmlinfobif.tax_system.ljust(25," ")+xmlinfobif.currency.ljust(25," ")+"  "+(cur_a.to_i.to_s).rjust(20," ")+(cur_a.to_i.to_s).rjust(20," ")+" ".ljust(71," ")+xmlinfobif.description.ljust(255," ")+st_dt1.ljust(8," ")+sv_dt1.ljust(8," ")+xmlinfobif.voucher_no.to_s.ljust(15," ")+xmlinfobif.period.to_s.ljust(6," ")+" ".ljust(1277," ")+"\n"  
     end
    
    File.open("C:/rdd.dat", "w")  { |f| f.write(bifstr) }

    @kn_content = bifstr 
    @kn_content_type='text/plain'
    @response.headers["Content-Type"] = @kn_content_type
    render :layout=>false
    # redirect_to :action => :index,:controller=>'bifinfo' 
  end 
  

     
end
